package com.haixiaoke.saas.department.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.haixiaoke.saas.common.config.OssConfig;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.oss.AliOssServiceImpl;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.department.domain.WechatAccount;
import com.haixiaoke.saas.department.mapper.WechatAccountMapper;
import com.haixiaoke.saas.department.service.IWechatAccountService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * 微信商户号信息Service业务层处理
 *
 * @author xiaolong
 * @date 2023-12-26
 */
@Service
public class WechatAccountServiceImpl implements IWechatAccountService {
    @Autowired
    private WechatAccountMapper wechatAccountMapper;
    @Autowired
    private AliOssServiceImpl aliOssService;
    @Autowired
    private OssConfig ossConfig;

    /**
     * 查询微信商户号信息
     *
     * @return 微信商户号信息
     */
    @Override
    public WechatAccount selectWechatAccountByWechatAccountId() {
        return wechatAccountMapper.selectWechatAccountByTenantId(TenantContext.getTenantId());
    }

    /**
     * 查询微信商户号信息列表
     *
     * @param wechatAccount 微信商户号信息
     * @return 微信商户号信息
     */
    @Override
    public List<WechatAccount> selectWechatAccountList(WechatAccount wechatAccount) {
        return wechatAccountMapper.selectWechatAccountList(wechatAccount);
    }

    /**
     * 新增微信商户号信息
     *
     * @param wechatAccount 微信商户号信息
     * @return 结果
     */
    @Override
    public int insertWechatAccount(WechatAccount wechatAccount) {
        wechatAccount.setCreateTime(DateUtils.getNowDate());
        return wechatAccountMapper.insertWechatAccount(wechatAccount);
    }

    /**
     * 修改微信商户号信息
     *
     * @param wechatAccount 微信商户号信息
     * @param file
     * @return 结果
     */
    @Override
    public int updateWechatAccount(WechatAccount wechatAccount, MultipartFile file) {


        if (wechatAccount.getAccountApiKey().length() != 32) {
            throw new ServiceException("API秘钥不是32位");
        }
        if (file != null) {
            String photoUrl = aliOssService.upload(ossConfig.getAccountCertificate(), file.getOriginalFilename(), file);
            wechatAccount.setAccountCertificate(photoUrl);
        }

        if (wechatAccount.getWechatAccountId() == null) {
            WechatAccount wechatAccount1 = new WechatAccount();
            BeanUtil.copyProperties(wechatAccount, wechatAccount1);
            wechatAccount1.setTenantId(TenantContext.getTenantId());
            wechatAccount1.setCreateTime(DateUtils.getNowDate());
            return wechatAccountMapper.insertWechatAccount(wechatAccount1);
        }

        // 删除原始oss文件
        WechatAccount account = wechatAccountMapper.selectWechatAccountByWechatAccountId(wechatAccount.getWechatAccountId());
        String accountCertificate = account.getAccountCertificate();
        if (!StringUtils.isBlank(accountCertificate)) {
            aliOssService.delete(accountCertificate);
        }
        wechatAccount.setUpdateTime(DateUtils.getNowDate());
        return wechatAccountMapper.updateWechatAccount(wechatAccount);
    }

    /**
     * 批量删除微信商户号信息
     *
     * @param wechatAccountIds 需要删除的微信商户号信息主键
     * @return 结果
     */
    @Override
    public int deleteWechatAccountByWechatAccountIds(Long[] wechatAccountIds) {
        return wechatAccountMapper.deleteWechatAccountByWechatAccountIds(wechatAccountIds);
    }

    /**
     * 删除微信商户号信息信息
     *
     * @param wechatAccountId 微信商户号信息主键
     * @return 结果
     */
    @Override
    public int deleteWechatAccountByWechatAccountId(Long wechatAccountId) {
        return wechatAccountMapper.deleteWechatAccountByWechatAccountId(wechatAccountId);
    }

    @Override
    public WechatAccount selectWechatAccountByTenantId(String tenantId) {
        return wechatAccountMapper.selectWechatAccountByTenantId(tenantId);
    }

    @Override
    public WechatAccount selectWechatAccountByAccountId(String accountId) {
        return wechatAccountMapper.selectWechatAccountByAccountId(accountId);
    }

}
